<?php

namespace catchAdmin\registration\controller;

use app\common\utils\Upfile;
use catchAdmin\registration\request\PhoneRequest;
use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\FollowResultDict as FRDModel;
use catcher\exceptions\FailedException;
use Jiemo\Mobile\Parser;
use Ofan\PhoneLocation;
use think\exception\ValidateException;
use think\facade\Filesystem;
use think\File;
use think\Request;

class Flresult extends CatchController
{
    protected $frd;

    public function __construct(FRDModel $frd)
    {
        $this->frd = $frd;
    }

    /**
     * 列表
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(): \think\response\Json
    {
        return CatchResponse::paginate($this->frd->getLists());
    }

    /**
     * 保存
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function save(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        //是否存在数据
        $is_existence = $this->frd->getOneData(['deleted_at' => 0], 'code', 'created_at');

        //存在数据，在原code上+1
        if ($is_existence) {
            $req["code"] = $is_existence['code'] + 1;
        } else {
            $req["code"] = 1;
        }
        return CatchResponse::success($this->frd->addData($req));
    }

    /**
     * 更新
     *
     * @time 2020年01月09日
     * @param $id
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update($id, CatchRequest $request): \think\response\Json
    {
        return CatchResponse::success($this->frd->saveData($id, $request->param()));
    }

    /**
     * 删除
     *
     * @time 2020年01月09日
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id): \think\response\Json
    {
        return CatchResponse::success($this->frd->delData($id));
    }

    /**
     * 获取全部数据
     * @return \think\response\Json
     */
    public function get_follow_result(): \think\response\Json
    {
        return CatchResponse::success($this->frd->getConditionData([], 'code,name,category,is_effective'));
    }

    public function uploadRecord(Request $request)
    {
        $date = date("Ymd");
        $file = $request->file();
        try {
            validate(['filedata1' => 'fileExt:wav'])
                ->check($file);
            $fileName = $file["filedata1"]->getOriginalName();
            $uploadResult = \think\facade\Filesystem::putFileAs('phone_record/' . $date, $file['filedata1'], $fileName);
            return CatchResponse::success(["path" => $uploadResult]);
        } catch (ValidateException $exception) {
            return CatchResponse::fail($exception->getMessage());
        }

    }

    public function getHomeOfTheMobile(PhoneRequest $request): \think\response\Json
    {
        $mobile = $request->param("mobile", "", "");
        $result = [
            "sp" => "未知",
            "province" => "未知",
            "city" => "未知",
            "zip_code" => "未知",
            "area_code" => "未知",
            "is_local" => 0
        ];
        try {
            $parser = new PhoneLocation();
            $result = $parser->find($mobile);
            if (!empty($result)) {
                $result["is_local"] = $result["area_code"] === "0431" ? 1 : 0;
            }
        } catch (\Exception $exception) {
            return CatchResponse::success($result);
        }
        return CatchResponse::success($result);

    }
}